package gui;

import entity.ExamInfo;
import entity.QuestionInfo;
import entity.User;
import exception.NameOrPasswordException;

import javax.swing.JOptionPane;

import service.ExamService;

/**
 * 客户端界面控制器
 * 
 * @author Administrator
 * 
 */
public class ClientContext {
	private WelcomeWindow window;
	private ExamService service;
	private LoginFrame loginFrame;
	private MenuFrame menuFrame;
	private ExamFrame examFrame;
	private User user;
	private ExamInfo examInfo;
	private QuestionInfo questionInfo;
	private int questionIndex = 0;
	private boolean isFinish = false;
	private int score = 0;
	public LoginFrame getLoginFrame() {
		return loginFrame;
	}

	/**
	 * 通过set方法注入LoginFrame对象
	 * 
	 * @param loginFrame
	 */
	public void setLoginFrame(LoginFrame loginFrame) {
		this.loginFrame = loginFrame;
	}

	/**
	 * 构造器依赖，依赖注入 创建当前的ClientContext实例，必须依赖ExamService的实例，也就是说
	 * 将ExamService的实例通过构造器注入到当前的对象中
	 * 
	 * @param service
	 *            ExamService的实例
	 */
	public ClientContext(ExamService service) {
		this.service = service;
	}

	public MenuFrame getMenuFrame() {
		return menuFrame;
	}

	public void setMenuFrame(MenuFrame menuFrame) {
		this.menuFrame = menuFrame;
	}

	public ExamFrame getExamFrame() {
		return examFrame;
	}

	public void setExamFrame(ExamFrame examFrame) {
		this.examFrame = examFrame;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setWindow(WelcomeWindow window) {
		this.window = window;
	}
	
	public ExamInfo getExamInfo() {
		return examInfo;
	}

	public QuestionInfo getQuestionInfo() {
		return service.getQuestion(questionIndex);
	}
	/**显示欢迎窗体，程序的开始方法*/
	public void showWelcome() {
		window.setVisible(true);
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		window.setVisible(false);
		loginFrame.showView();
	}
	/**登录面板的登录按钮的方法*/
	public void login() {
		int id = loginFrame.getId();
		String pwd = loginFrame.getPwd();
		try {
			user = service.login(id, pwd);
			loginFrame.setVisible(false);
			menuFrame.showView();
			menuFrame.upadateView();
		} catch (NameOrPasswordException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(loginFrame, "用户名或密码错误！");
		}
	}
	/**选项面板的开始按钮的方法*/
	public void start() {
		if (isFinish) {
			JOptionPane.showMessageDialog(null, "您已经考过试，不能重复考试！");
			return;
		}
		examInfo = service.start();
		questionInfo = service.getQuestion(0);
		menuFrame.setVisible(false);
		examFrame.updateView();
		examFrame.showView();
	}

	/**正常的退出方法*/
	public void exit() {
		int result = JOptionPane.showConfirmDialog(null, "您确定要退出该系统吗？", "离开系统",
				JOptionPane.YES_NO_OPTION);
		if (result == JOptionPane.OK_OPTION) {
			System.exit(0);
		}
	}
	/**未考试时的退出方法*/
	public void menuExit() {
		if (isFinish) {
			exit();
		} else {
			int result = JOptionPane.showConfirmDialog(menuFrame,
					"您还没有考试，确定要离开吗？");
			if (result == JOptionPane.YES_OPTION) {
				System.exit(0);
			}
		}
	}
	/**考试窗口的下一题方法*/
	public boolean next() {
		service.sendAnswers(questionIndex, examFrame.getUserAnswer());
		if (questionIndex + 1 == examInfo.getQuestionCount()) {
			return false;
		}
		questionIndex++;
		examFrame.updateQuestions();
		return true;
	}

	/**考试窗口的上一题方法*/
	public boolean prev() {
		service.sendAnswers(questionIndex, examFrame.getUserAnswer());
		if (questionIndex == 0) {
			return false;
		}
		questionIndex--;
		examFrame.updateQuestions();
		return true;
	}

	/**考试窗体的交卷方法*/
	public void send() {
		service.sendAnswers(questionIndex, examFrame.getUserAnswer());
		int result = JOptionPane.showConfirmDialog(examFrame, "您确定要交卷吗？");
		if (result == JOptionPane.YES_OPTION) {
			score = service.commit();
			gameOver();
		}
	}
	/**考试结束时的方法*/
	public void gameOver(){
		showScore();
		examFrame.setVisible(false);
		menuFrame.showView();
		isFinish = true;
	}
	/**选项窗体的方法*/
	public void result() {
		if (isFinish) {
			showScore();
		} else {
			JOptionPane.showMessageDialog(menuFrame, "您还没有进行考试！请考后再查！");
		}
	}
	/**选项窗体的显示分数方法*/
	public void showScore() {
		String message = "";
		if (score == 100) {
			message = "返璞归真";
		} else if (score >= 90) {
			message = "笑傲江湖";
		} else if (score >= 80) {
			message = "惊才绝艳";
		} else if (score >= 70) {
			message = "鹤立鸡群";
		} else if (score >= 60) {
			message = "超乎寻常";
		}else {
			message = "继续努力";
		}
		JOptionPane.showMessageDialog(examFrame, message+"!您的分数是" + score+"分");
	}
	/**选项窗体的显示考试规则的方法*/
	public void message() {
		JOptionPane.showMessageDialog(menuFrame, service.getRule());
	}
	/**考试窗体的选题按钮的方法*/
	public void choose(String text) {
		int index = 0;
		try {
			index = Integer.parseInt(text);
			if (index < 1 || index > examInfo.getQuestionCount()) {
				throw new Exception();
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "请输入正确的题号。如：10");
			return;
		}
		service.sendAnswers(questionIndex, examFrame.getUserAnswer());
		questionIndex = index - 1;
		examFrame.updateQuestions();
	}
}
